package com.activities;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.Utility.Configs;
import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.adapters.VaultFileAdapter;
import com.controls.ws.VaultWSControl;
import com.dialogs.LoadingDialog;
import com.events.WebServiceCommunicatorListener;
import com.objects.VaultFile;
import com.senditcertified.mail.R;

public class VaultFileListActivity extends BaseActivity implements
		OnItemClickListener, WebServiceCommunicatorListener {

	private LoadingDialog mDialog;
	private ListView mLstViewFile;
	private VaultFileAdapter mAdapter;
	private VaultWSControl mWSVault;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {

		String fileName = mAdapter.getItem(position).getName();

		if (fileName != null && fileName.length() > 0) {
			File file = new File(Configs.FILE_STORE + fileName);
			if (file.exists()) {
				String type = "";

				Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
				Uri uri = Uri.fromFile(file);
				Log.e("FILE Uri:", uri.toString());
				String extension = MimeTypeMap.getFileExtensionFromUrl(file
						.getAbsolutePath());
				if (extension != null) {
					MimeTypeMap mime = MimeTypeMap.getSingleton();
					type = mime.getMimeTypeFromExtension(extension);
					Log.e("FILE MIME:", type + "");
				}
				if (TextUtils.isEmpty(type))
					intent.setDataAndType(Uri.fromFile(file), "*/*");
				else
					intent.setDataAndType(Uri.fromFile(file), type);

				try {
					startActivity(intent);
				} catch (ActivityNotFoundException e) {
					e.printStackTrace();
					Utility.showAlert(getParent(),
							"Application not found to open file.");
				}

			} else {
				if (mWSVault == null)
					mWSVault = new VaultWSControl(this, this);
				mWSVault.getWebInterfaceVaultUrl(Utility.getStringPreference(
						getParent(), Utility.KEY_LOGIN_TOKEN, "1"), fileName);
			}
		}

	}

	@Override
	protected int initLayout() {
		return R.layout.activity_vault_open_file_list;
	}

	@Override
	protected void initViews() {
		mLstViewFile = (ListView) findViewById(R.id.list);
	}

	@Override
	protected void initComponents() {
		ArrayList<VaultFile> list = new ArrayList<VaultFile>();
		try {
			Bundle extra = getIntent().getExtras();
			if (extra != null) {
				list.addAll((Collection<? extends VaultFile>) (getIntent()
						.getSerializableExtra("open")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (list != null && list.size() > 0) {
			mAdapter = new VaultFileAdapter(VaultFileListActivity.this, list);
			mLstViewFile.setAdapter(mAdapter);
		}

	}

	@Override
	protected void addListener() {
		mLstViewFile.setOnItemClickListener(this);
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		mDialog = new LoadingDialog(getParent());
		mDialog.setCancelable(false);
		mDialog.show();

	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		MessageManager.showToast(this, error);

	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}

		if (flag == WebServiceFlag.GET_WEB_INTERFACE_VAULT_URL) {
			// TODO
			String link = mWSVault.parseAction(response,
					"GetWebInterfaceVaultURLResult");
			if (link != null && link.startsWith("http")) {
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
				startActivity(i);
			}
		}

	}

	@Override
	protected void onDestroy() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		if (mWSVault != null) {
			mWSVault.cancel();
			mWSVault = null;
		}
		super.onDestroy();
	}

}
